import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import { Models } from '@/common/typings/modle'
import { command } from '@/server/mysql'
import { Success } from '@/core/HttpException'
import { addUpdate, getUpdateContent } from '@/common/utils/sql'
import validator from '@/middlewares/validator'
import schema from '@/common/api-json-schema/system/common/update'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/resource`,
})
router.post('/update', verifyToken, validator(schema, 'body'), async (ctx: Models.Ctx) => {
  const { label, uniqueProp, parentUniqueProp, type, status, id } = ctx.request.body as any
  const fieldObj = {
    label,
    uniqueProp,
    parentUniqueProp,
    type,
    status,
  }
  const result = (
    await command(
      addUpdate(`UPDATE system_resource SET ${getUpdateContent(fieldObj)} WHERE id = '${id}'
 ;`),
      true,
    )
  ).results
  if (result.affectedRows === 1) {
    throw new Success({ data: result, msg: '修改成功' })
  }
})
export default router
